package cn.aiayun.springactivity.controller;

import cn.hutool.core.io.IoUtil;
import com.sun.imageio.plugins.common.ImageUtil;
import lombok.extern.slf4j.Slf4j;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.image.ProcessDiagramGenerator;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 傲寒
 * @date 2022/12/29
 */
@Slf4j
@RestController
public class FlowChartController {

    @Autowired
    protected ProcessEngine processEngine;

    @Autowired
    protected RuntimeService runtimeService;

    @Autowired
    protected RepositoryService repositoryService;

    @Autowired
    protected TaskService taskService;

    @Autowired
    protected HistoryService historyService;

    @Autowired
    protected ProcessDiagramGenerator defaultProcessDiagramGenerator;


    @RequestMapping(value = "/image", produces = {MediaType.IMAGE_PNG_VALUE})
    @ResponseBody
    public byte[] getImage(@RequestParam String processInstanceId) throws IOException {
        InputStream inputStream = getDiagram(processInstanceId);
        //File file = new File("C:\\Users\\pn20120160\\Desktop\\图片\\ai.png");
        //FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[inputStream.available()];
        inputStream.read(bytes, 0, inputStream.available());
        return bytes;
    }


    //查看流程图
    //@RequestMapping(value = "/image", method = RequestMethod.GET)
    //public void image(HttpServletResponse response, @RequestParam String processInstanceId) {
    //    try {
    //        InputStream is = getDiagram(processInstanceId);
    //        if (is == null)
    //            return;
    //        response.setContentType("image/png");
    //        BufferedImage image = ImageIO.read(is);
    //        OutputStream out = response.getOutputStream();
    //        ImageIO.write(image, "png", out);
    //
    //        is.close();
    //        out.close();
    //    } catch (Exception ex) {
    //        log.error("查看流程图失败", ex);
    //    }
    //}


    public InputStream getDiagram(String processInstanceId) {
        //获得流程实例
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        if (Objects.isNull(processInstance)) {
            return null;
        }
        String processDefinitionId = processInstance.getProcessDefinitionId();
        //List<HistoricActivityInstance> historicActivityInstances = historyService.createHistoricActivityInstanceQuery()
        //        .processInstanceId(processInstanceId)
        //        .list();
        //List<String> activeActivityIds = historicActivityInstances.stream()
        //        .map(HistoricActivityInstance::getActivityId)
        //        .collect(Collectors.toList());
        //获取BPMN模型对象
        BpmnModel model = repositoryService.getBpmnModel(processDefinitionId);
        //获取流程实例当前的节点，需要高亮显示
        List<String> currentActs = runtimeService.getActiveActivityIds(processInstance.getId());


        return defaultProcessDiagramGenerator.generateDiagram(model,
                currentActs,
                new ArrayList<>(),
                "宋体",
                "宋体",
                null,
                false);

    }


}
